const express = require("express");
const router = express.Router();

const MySql = require("../lib/mysql");


//formdate函数，用于修改日期格式
function formateDateB(datetime) {
    function addDateZero(num) {
        return (num < 10 ? "0" + num : num);
    }
    let d = new Date(datetime);
    let formatdatetime = d.getFullYear() + '-' + addDateZero(d.getMonth() + 1) + '-' + addDateZero(d.getDate()) ;
    return formatdatetime;
};
// function formateDate(datetime) {
//     function addDateZero(num) {
//         return (num < 10 ? "0" + num : num);
//     }
//     let d = new Date(datetime);
//     let formatdatetime = d.getFullYear() + '-' + addDateZero(d.getMonth() + 1) ;
//     return formatdatetime;
// };


router.post("/basic",async(req,res,next) => {

   try {

    //根据id提供用户信息
    const id = req.body.id;
    console.log(id);
    const resUser = await MySql.query('select * from user where id=?',[id]);
    const user = resUser[0] ;
    console.log(user);
    var ub = formateDateB(user.birthday) ;
    res.json({
        //basic
        id:user.id,
        name:user.name,
        sex:user.sex,
        password:user.password,
        phone:user.phone,
        email:user.email,
        specialty:user.specialty,
        college:user.college,
        photourl:user.photourl,
        birthday:ub,
        homeland:user.homeland,
        politic:user.politic,
        nationality:user.nationality,
        skills:user.skills,
        evaluate:user.evaluate,
        others:user.others,
        code:1,
        message:'获取成功',

    })
   } catch (error) {
    console.log(error)
    res.json({
        code: -1,
        message: '服务器错误',
    })  
   }
});

router.post("/edu",async(req,res,next) => {
    const id = req.body.id;
    const resEdu = await MySql.query('select * from education_exp where user_id=? && status =1',[id]);
    const edu = resEdu ;
    console.log(edu);
    // var es = formateDate(edu.start_time);
    // var ee = formateDate(edu.end_time);
    res.json({
        //edu
        edu:resEdu,
        // edu_start_time:es,
        // edu_end_time:ee,
        // diploma:edu.diploma,
        // edu_exp:edu.education_exp,
        code:1,
        message:'获取成功',

    })
});


router.post("/practice",async(req,res,next) => {
    const id = req.body.id;
    const resPra = await MySql.query('select * from practice_exp where user_id=? && status =1',[id]);
    const pra =resPra;
    // var pas = formateDate(pra.start_time);
    // var pae = formateDate(pra.end_time);

    res.json({
        //practice
        pra:pra,
        // practice_exp:pra.practice_exp,
        // pra_start_time:pas,
        // pra_end_time:pae,
        // company:pra.company,
        code:1,
        message:'获取成功',

    })
});


router.post("/project",async(req,res,next) => {
    const id = req.body.id;
    const resPro = await MySql.query('select * from project_exp where user_id=? && status =1',[id]);
    const pro = resPro ;
    // var pos = formateDate(pro.start_time);
    // var poe = formateDate(pro.end_time);

    res.json({
        //project
        pro:pro,
        // project_exp:pro.project_exp,
        // pro_start_time:pos,
        // pro_end_time:poe,
        // title:pro.title,
        code:1,
        message:'获取成功',

    })
});

router.post("/honor",async(req,res,next) => {
    const id = req.body.id;
    const resHonor = await MySql.query('select * from honor where user_id=? && status =1',[id]);
    const honor = resHonor ;

    res.json({
        honor:honor,
        code:1,
        message:'获取成功',

    })
});


// router.post("/skills",async(req,res,next) => {
//     const id = req.body.id;
//     const resSkills = await MySql.query('select * from skills where user_id=? && status =1',[id]);
//     const skills = resSkills;
//     res.json({
//         //skills
//         skills:skills,
//         code:1,
//         message:'获取成功',
//     })
// });
// router.post("/evaluate",async(req,res,next) => {
//     const id = req.body.id;
//     const resEvaluate = await MySql.query('select * from evaluate where user_id=? && status =1',[id]);
//     const evaluate = resEvaluate ;
//     console.log(evaluate);
//     res.json({
//         //evaluate
//         evaluate:evaluate,
//         code:1,
//         message:'获取成功',
//     })
// });
// router.post("/others",async(req,res,next) => {
//     const id = req.body.id;
//     const resOthers = await MySql.query('select * from others where user_id=? && status =1',[id]);
//     const others =resOthers ;
//     res.json({
//         //others
//         others:others,
//         code:1,
//         message:'获取成功',
//     })
// });




module.exports = router;